import config from './config.js'
import {
  getStorage
} from "../api/Storage.js"
let ajaxTime = 0
const request = (params) => {
  let header = {}
  header['content-type'] = 'application/x-www-form-urlencoded'
  ajaxTime++
  if (params.url.includes("/my/")) {
    // 拼接header 带上token
    header["Authorization"] = getStorage("token")
  }

  return new Promise((resolve, reject) => {
    uni.request({
      url: `${config.host}${params.url}`,
      method: params.method || 'GET',
      data: params.data || {},
      header: header,
      success: (res) => {
        // if (res.data.meta.status == 200) {
        if (res.statusCode == 200) {
          resolve(res.data.message)
        } else {
          uni.showToast({
            title: res.data.message,
            icon: 'none',
          })
        }
      },
      fail: (err) => {
        reject(err)
        uni.showToast({
          title: '网络出现错误',
          icon: 'none',
        })
      },
      complete: () => {
        ajaxTime--
        if (ajaxTime == 0) {
          setTimeout(() => {
            uni.hideLoading()
          }, 300)
        }
      },
    })
  })
}

export default request;
